package neu.soft.listener;

import neu.soft.entity.DO.ReceiptJournal;
import neu.soft.facade.ReceiptFacade;
import neu.soft.vo.MsgBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 客户订单服务监听
 */
@Component
public class ReceiptListener {

    @Autowired
    ReceiptFacade receiptFacade;


    //监听出库，生成核算凭证，用于结账时和用户交易明细对比
    @StreamListener("financial-receipt-input")
    public void receiptAccounting(@Payload MsgBean<Map<String, Object>> msgBean) {

        Map<String, Object> map = msgBean.getData();
        String outOrderId = (String) map.get("");
        String oid = (String) map.get("oid");
        List<String> code = (List<String>) map.get("code");
        List<Integer> num = (List<Integer>) map.get("num");
        receiptFacade.receiptAccountingHandle(outOrderId, code, num, oid);
    }

    //监听订单完成，结账
    @StreamListener("financial-pay-input")
    public void finishOrder(@Payload MsgBean<String> msgBean){
        String oid = msgBean.getData();

        receiptFacade.generateSourceDocument(oid);
    }


    //监听客户支付,记录交易明细，方便之后进行核算
    @StreamListener("financial-journal-input")
    public void journal(@Payload MsgBean<ReceiptJournal> msgBean) {

        ReceiptJournal receiptJournal = msgBean.getData();
        receiptFacade.journal(receiptJournal);

    }

}
